<?php 
	
	class PromotionConverter{
		
		public static function convert($id, $name, $dateFrom, $dateTo, $value, $desc){
			$result = new Promotion();
			$result->setId($id);
			$result->setName($name);
			$result->setDateFrom($dateFrom);
			$result->setDateTo($dateTo);
			$result->setValue($value);
			$result->setDesc($desc);
			return $result;			
		}
		
	}	



	class PromotionFindCommand implements Command{
			public function __construct(PromotionSearchControls $controls){
				$this->controls = $controls;
				
			}
			public function doExecute($connection){
				$result = array();
				
				
				
				$query = "select pt." . BoaConstans::PROMOTION_COLUMN_ID . ", pt." 
				. BoaConstans::PROMOTION_COLUMN_NAME . ", pt."
				. BoaConstans::PROMOTION_COLUMN_DATE_FROM . ", pt."
				. BoaConstans::PROMOTION_COLUMN_DATE_TO . ", pt."
				. BoaConstans::PROMOTION_COLUMN_VALUE . ", pt."
				. BoaConstans::PROMOTION_COLUMN_DESCR .
				" from ". BoaConstans::PROMOTION_TABLE . " pt";				
			
				$where = null;
				$types = "";
				$params = array();
				
				if($this->controls->getName() != null){
					$types .="s";
					$params[] = $this->controls->getName();
					$this->addToWhere($where, "upper(pt." . BoaConstans::PROMOTION_COLUMN_NAME .") like upper(CONCAT('%', ?, '%'))");
			}
				
				
				//wyswietelenie promocji dla danego serwisu
				if($this->controls->getSid() != null){
					$types .="i";
					$params[] = $this->controls->getSid();
					$promotionServiceQuery  = "pt." .BoaConstans::PROMOTION_COLUMN_ID. " in (select pst." . BoaConstans::PROMOTION_SERVICE_COLUMN_PID . " FROM ".
					BoaConstans::PROMOTION_SERVICE_TABLE . " pst where " . BoaConstans::PROMOTION_SERVICE_COLUMN_SID . "=?)";					
					$this->addToWhere($where, $promotionServiceQuery);
				}
					
				//wyswietlenie promocji dla danego uzytkownika

				if(VariableUtils::isNumber($this->controls->getUid())){
					$types .="i";
					$params[] = $this->controls->getUid();
					$userServiceQuery = "pt." . BoaConstans::PROMOTION_COLUMN_ID . " in (select pst." . BoaConstans::PROMOTION_SERVICE_COLUMN_PID . " FROM " 
					. BoaConstans::PROMOTION_SERVICE_TABLE . " pst where pst." . BoaConstans::PROMOTION_SERVICE_COLUMN_SID .  " in (select ust." . BoaConstans::USER_SERVICE_COLUMN_SID . " FROM " .BoaConstans::USER_SERVICE_TABLE .
					" ust where ust." . BoaConstans::USER_SERVICE_COLUMN_UID . "=?))";
					$this->addToWhere($where, $userServiceQuery); 
					
				}
				
				 if($this->controls->getDateFrom() != null){
				 	
				 	$types .="s";
				 	$params[] = $this->controls->getDateFrom();
				 	$this->addToWhere($where, "pt." . BoaConstans::PROMOTION_COLUMN_DATE_FROM . " >=?");
				 	
				 				 	
				 }
				 
				 if($this->controls->getDateTo() != null){
				 	
				 	$types .="s";
				 	$params[] = $this->controls->getDateTo();
				 	$this->addToWhere($where, "pt." . BoaConstans::PROMOTION_COLUMN_DATE_TO . " >=?");
				 }
				
				 if($where != null){
					$query.=" where " . $where;				
				}
				
				$stmt = $connection->prepare($query);
				if($types != null && $types != ""){
					$bind_param = array();
					$bind_param[] = $types;
					for($i=0;$i<sizeof($params);$i++){
						$bind_param[] = &$params[$i];
					}
					call_user_func_array(array($stmt, 'bind_param'), $bind_param);
				}
				$stmt->execute();
	
				$id = null;
				$name = null;
				$dateFrom = null;
				$dateTo = null;
				$value = null;
				$desc = null;	
					
				$stmt->bind_result($id, $dateFrom, $dateTo, $name, $value, $desc);
				while($stmt->fetch()){
					$result[] = PromotionConverter::convert($id, $dateFrom, $dateTo, $name, $value, $desc);
				}
				$stmt->close();
								
				return $result;
		}
		
	private function addToWhere(&$where, $condition){
			if($where == null){
					$where = $condition;
			}else{
				$where.= " and " . $condition;
			}
		}
	
						
}
			
			

	class PromotionAddCommand implements Command{
		private $promotion;
		public function __construct(Promotion $promotion){
			$this->promotion = $promotion;			
		}
		
		public function doExecute($connection){
			
			if($this->promotion == null || !VariableUtils::isNumber($this->promotion->getValue())){
				return false;
			}
			
			$query = "insert into " . BoaConstans::PROMOTION_TABLE . "(" .
			BoaConstans::PROMOTION_COLUMN_NAME . "," .
			BoaConstans::PROMOTION_COLUMN_DATE_FROM . "," .
			BoaConstans::PROMOTION_COLUMN_DATE_TO . "," .
			BoaConstans::PROMOTION_COLUMN_DESCR . "," .
			BoaConstans::PROMOTION_COLUMN_VALUE .  
			") values(?,?,?,?,?)"; 
			
			$df = $this->promotion->getDateFrom();
			$dt = $this->promotion->getDateTo();
			$v = $this->promotion->getValue();
			$n = VariableUtils::getRealEscapeString($this->promotion->getName());
			$desc = VariableUtils::getRealEscapeString($this->promotion->getDesc());
				
				
			$stmt = $connection->prepare($query);
			$stmt->bind_param("ssssi", $n, $df, $dt, $desc, $v );
			$stmt->execute();
			$stmt->close();
			return mysqli_insert_id($connection);
			
				
		
		}
		
	}

	class PromotionDeleteCommand implements Command{
		private $id;

		public function __construct($promotionId){
			$this->id = $promotionId;
		}
		
		
		public function doExecute($connection){
		if($this->id == null || !VariableUtils::isNumber($this->id)){
				return false;
			}
			
			$query = "delete from " . BoaConstans::PROMOTION_TABLE . " where " . BoaConstans::PROMOTION_COLUMN_ID . "= ?";
			$stmt = $connection->prepare($query);
			$stmt->bind_param("i", $this->id);
			$stmt->execute();
			$ar = $stmt->affected_rows;
			$stmt->close();
			return $ar > 0 ? true : false;
		}
	}
	
	class PromotionModifyCommand implements Command{
		private $promotion;
		public function __construct(Promotion $promotion){
			$this->promotion = $promotion;
		}
		
		public function doExecute($connection){
			if($this->promotion == null){	
				return false;
			}
			
			if($this->promotion->getId() == null){
				// modyfikacja promocji ktora nie posiada identyfikatora, traktowana jako dodawanie
				$pac = new PromotionAddCommand($this->promotion);
				return $pac->doExecute($connection);
			}
			
			
			$df = $this->promotion->getDateFrom();
			$dt =$this->promotion->getDateTo();
			$v = $this->promotion->getValue();
			$n = VariableUtils::getRealEscapeString($this->promotion->getName());
			$desc = VariableUtils::getRealEscapeString($this->promotion->getDesc());
				
			$id = null;
			if(VariableUtils::isNumber($this->promotion->getId())){
				$id = $this->promotion->getId();
			}else{
				return false;
			}
			
			 $query = "update " . BoaConstans::PROMOTION_TABLE . " set " .
			BoaConstans::PROMOTION_COLUMN_DATE_FROM	  . "=?, " .
			BoaConstans::PROMOTION_COLUMN_DATE_TO  . "=?, " .
			BoaConstans::PROMOTION_COLUMN_VALUE  . "=?, " .
			BoaConstans::PROMOTION_COLUMN_NAME . "=?, " .
			BoaConstans::PROMOTION_COLUMN_DESCR . "=? where " .
			BoaConstans::PROMOTION_COLUMN_ID . "=?";			
			
			$stmt = $connection->prepare($query);
			$stmt->bind_param("ssissi", $df, $dt, $v, $n, $desc, $id);
			$stmt->execute();
			$ar = $stmt->affected_rows;
			$stmt->close();
			
			return $ar > 0 ? true : false;
		}
	}	
		class PromotionGetCommand implements Command{
			private $id;
			public function __construct($id){
				$this->id =$id;
			}
			public function doExecute($connection){
				$query = "select ".
				BoaConstans::PROMOTION_COLUMN_NAME . ", ".
				BoaConstans::PROMOTION_COLUMN_DATE_FROM . ", ".
				BoaConstans::PROMOTION_COLUMN_DATE_TO . ", ".
				BoaConstans::PROMOTION_COLUMN_VALUE . ", ".
				BoaConstans::PROMOTION_COLUMN_DESCR . " from " .
				BoaConstans::PROMOTION_TABLE. " where " . BoaConstans::PROMOTION_COLUMN_ID. "=?";
				
				$stmt = $connection->prepare($query);
				$stmt->bind_param("i", $this->id);
				$stmt->execute();
				
				$name = null;
				$dateFrom = null;
				$dateTo = null;
				$value = null;
				$desc = null;
				$stmt->bind_result($name, $dateFrom, $dateTo, $value, $desc);
				$stmt->fetch();
				$result = PromotionConverter::convert($this->id, $name, $dateFrom, $dateTo, $value, $desc);
				$stmt->close();				
			
				return $result;
			}
			
		}
	
?>